package cn.fl.npspl.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.npspl.service.ISplNpSocialRlService;
import cn.fl.npspl.transform.SplNpSocialRlVOTransform;
import cn.fl.npspl.vo.SplNpSocialRlVO;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.npspl.api.ISplNpSocialRlApiService;
import fl.spl.facade.npspl.dto.SplNpSocialRlDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description: 自然人经销商社会关系表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class SplNpSocialRlServiceImpl implements ISplNpSocialRlService {

    @Autowired
    private ISplNpSocialRlApiService splNpSocialRlApiService;
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    
    @Override
    public SplNpSocialRlVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "获取对象主键不能为空!");
        }
        //TODO 业务逻辑
        SplNpSocialRlDTO dto = null;//this.自定义ApiService.selectSplNpSocialRlDTOById(id);
        return SplNpSocialRlVOTransform.toVO(dto);
    }

    @Override
    public Long add(SplNpSocialRlVO entity) throws BaseException {
        if (null == entity) {
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "新增对象不存在!");
        }
        ;
        //return this.splNpSocialRlApiService.add(SplNpSocialRlVOTransform.toDTO(entity));
        //TODO 业务逻辑
        return null;
    }

    @Override
    public boolean removeById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "删除对象主键不能为空!");
        }
        ;
        return this.splNpSocialRlApiService.removeSplNpSocialRlDTOById(id, SplSystemConstant.FL_SYS_CD, CurrentThreadContext.getCurrentOrgCd(), SplSystemConstant.FL_WEB_CHL_CD);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) {
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "删除对象主键不能为空!");
        }
        ;
        return false;
    }

    @Override
    public boolean modify(SplNpSocialRlVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.newWebLogger("修改对象不能为空!").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "修改对象不能为空!");
        }
        return this.splNpSocialRlApiService.modifySplNpSocialRlDTO(SplNpSocialRlVOTransform.toDTO(entity), 
        		CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), SplSystemConstant.FL_WEB_CHL_CD);
    }

    @SuppressWarnings("unchecked")
	@Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) {
            page = new Pager();
        }
        Pager pager = this.splNpSocialRlApiService.searchSplNpSocialRlListPage(page,CurrentThreadContext.getFlServiceSysCd()
        		, CurrentThreadContext.getCurrentOrgCd(), SplSystemConstant.FL_WEB_CHL_CD);
        List<SplNpSocialRlDTO> list = (List<SplNpSocialRlDTO>) pager.getResultList();

        List<SplNpSocialRlVO> voList = SplNpSocialRlVOTransform.toVOList(list);
        pager.setResultList(voList);

		return pager;
    }

    @Override
    public Pager searchWithEncryptListPage(Pager page) throws BaseException {
        if (null == page) {
            page = new Pager();
        }
        Pager pager = this.splNpSocialRlApiService.searchSplNpSocialRlWithEncryptListPage(page,CurrentThreadContext.getFlServiceSysCd()
                , CurrentThreadContext.getCurrentOrgCd(), SplSystemConstant.FL_WEB_CHL_CD);
        List<SplNpSocialRlDTO> list = (List<SplNpSocialRlDTO>) pager.getResultList();

        List<SplNpSocialRlVO> voList = SplNpSocialRlVOTransform.toVOList(list);
        pager.setResultList(voList);

        return pager;
    }
    
    @Override
	public List<KeyValueVO> searchCertTypCdList() throws BaseException{
		return this.getKvList(DictionaryConstant.CODE_GRP_CERT_TYP_CD);
	}
	
	@Override
	public List<KeyValueVO> searchRlTypCdList(String code) throws BaseException{
		return this.getKvList(code);
	}
	
	/**
	 * 根据字典中代码组值获取组员集合
	 * @param grpCd 代码组值
	 * @return 集合
	 */
	@Override
	public List<KeyValueVO> getKvList(String grpCd){
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
            BaseLogUtils.newWebLogger("获取字典项组失败,业务异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(e)
                    .warn(LogExceptionEnum.COMMON_CODE_0001);
        } catch (Exception e) {
            BaseLogUtils.newWebLogger("获取字典项组失败,系统异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(e)
                    .error(LogExceptionEnum.COMMON_CODE_0099);
        }
		return kvList;
	}
}
